/**
 * Created by admin on 2017/9/5.
 */
define(['app', 'filters/filters'], function(app) {
    app.register.controller('informationEntry',['$scope','$state','$stateParams','$rootScope','$Ajax','$http', function($scope, $state, $stateParams, $rootScope,$Ajax,$http) {
        var http = app._baseUrl;
        $scope.load = {
            _getInitial: function() { //专业类别和专业
                var url = http + '/Pub/Public/postList';
                var data = {};
                $Ajax.postData(url,data).then(function(res){
                    if(res.data.success){
                        $scope.cate_idlist = res.data.data;
                    }
                });
            },
            init: function() {
                this._getInitial && this._getInitial();
                $scope.birY = '';//初始化日历插件
                $scope.birM = '';
                setTimeout(function () {
                    $scope.initjquery();
                },200)
            }
        };
        $scope.load.init && $scope.load.init();

    //   ------------------------------------------------温馨提示弹窗
        $scope.popupModal = {//做一步判断，判断用户是否有未填写信息
            tipModal: false,
            showTip:false,
            birM:'',
            birY:'',
            obj:{},
            message:'',
            birthday:'',
            hide: function () {
                this.tipModal = false;
            },
            show: function () {
                this.showTip = false;
                this.birM = $scope.birM;
                this.birY = $scope.birY;
                if(this.birM < 10){
                    this.birM = '0'+ $scope.birM;
                }
                this.birthday = this.birY + '-' + this.birM;
                this.obj = {
                    phone:$scope.phone,
                    sex:$scope.sex,
                    real_name:$scope.real_name,
                    birthday:this.birthday,
                    school:$scope.school,
                    education:$scope.education,
                    major:$scope.major,
                    cate_id:$scope.cate_id
                };
                if(this.showMessage()){
                    this.tipModal = true;
                }

            },
            showMessage:function(){
                var exp=/^1[3|4|5|7|8][0-9]{9}$/;
                if($scope.sex&&this.obj.real_name&&this.obj.phone&&$scope.birM&&this.obj.major&&$scope.birY&&this.obj.school&&this.obj.education&&this.obj.cate_id&&exp.test(this.obj.phone)==true){
                    return true;
                }
                if(!this.obj.real_name){
                    this.message = '真实姓名不能为空';
                    this.showTip = true;
                    return false;
                }
                if(!this.obj.phone){
                    this.message = '账号码不能为空';
                    this.showTip = true;
                    return false;
                }
                if(!$scope.sex){
                    this.message = '性别不能为空';
                    this.showTip = true;
                    return false;
                }
                if(!$scope.birM||!$scope.birY){
                    this.message = '出生年月不能为空';
                    this.showTip = true;
                    return false;
                }
                if(!this.obj.school){
                    this.message = '毕业学校不能为空';
                    this.showTip = true;
                    return false;
                }
                if(!this.obj.education){
                    this.message = '学历不能为空';
                    this.showTip = true;
                    return false;
                }
                if(!this.obj.major){
                    this.message = '专业不能为空';
                    this.showTip = true;
                    return false;
                }
                if(!this.obj.cate_id){
                    this.message = '测评岗位不能为空';
                    this.showTip = true;
                    return false;
                }
                if(exp.test(this.obj.phone)==false){
                    this.message = '账号格式错误';
                    this.showTip = true;
                    return false;
                }
            },
            focus:function(){
                if(this.showTip==true){
                    this.showTip = false;
                    return false;
                }
            }
        };

    //-------------------------------------------------------出生年月插件
        $scope.initjquery =function () {
            (function($){
                $.extend({
                    ms_DatePicker: function (options) {
                        var defaults = {
                            YearSelector: "#sel_year",
                            MonthSelector: "#sel_month",
                            YFirstText: $scope.birY,
                            MFirstText: $scope.birM,
                            FirstValue: 1
                        };
                        var opts = $.extend({}, defaults, options);
                        var $YearSelector = $(opts.YearSelector);
                        var $MonthSelector = $(opts.MonthSelector);
                        // var $DaySelector = $(opts.DaySelector);
                        var YFirstText = opts.YFirstText;
                        var MFirstText = opts.MFirstText;
                        // var DFirstText = opts.DFirstText;
                        var FirstValue = opts.FirstValue;

                        // 初始化
                        var Ystr = "<option value=\"" + FirstValue + "\">"+YFirstText+"</option>";
                        var Mstr = "<option value=\"" + FirstValue + "\">"+MFirstText+"</option>";
                        $YearSelector.html(Ystr);
                        $MonthSelector.html(Mstr);

                        // 年份列表
                        var yearNow = new Date().getFullYear();
                        var yearSel = $YearSelector.attr("ng-model");
                        for (var i = yearNow; i >= 1900; i--) {
                            var sed = yearSel==i?"selected":"";
                            var yearStr = "<option value=\"" + i + "\" " + sed+">"+i+"</option>";
                            $YearSelector.append(yearStr);
                        }

                        // 月份列表
                        var monthSel = $MonthSelector.attr("ng-model");
                        for (var i = 1; i <= 12; i++) {
                            var sed = monthSel==i?"selected":"";
                            if(i < 10){
                                var monthStr = "<option value=\"" + i + "\" "+sed+">"+'0'+i+"</option>";
                            }else{
                                var monthStr = "<option value=\"" + i + "\" "+sed+">"+i+"</option>";
                            }
                            $MonthSelector.append(monthStr);
                        }
                        $MonthSelector.change(function () {
                            // BuildDay();
                        });
                        $YearSelector.change(function () {
                            // BuildDay();
                        });
                    }
                });
            })(jQuery);
            $.ms_DatePicker({
                YearSelector: ".sel_year",
                MonthSelector: ".sel_month"
            });
        };

    //保存用户填写信息
        $scope.save = {
            obj:{},
            token:$stateParams.token,
            reques:function(){
                this.obj = $scope.popupModal.obj;
                var url = $state.href('app.reviewtheTopic',{obj:JSON.stringify(this.obj),token:this.token});
                window.location.href = url;
                // window.close();
            }
        }
    }]);
})